import dbconfig from '../conf/dbconfig'
import path from 'path'
import { Dialect } from 'sequelize'
import { Sequelize } from 'sequelize-typescript'

class BaseDao {
  static baseDao: BaseDao = new BaseDao()
  sequelize!: Sequelize
  constructor() {
    this.initSeqConf('mysql')
  }
  initSeqConf(dialect: Dialect) {
    const { host, user, password, database, port } = dbconfig.getConf()
    this.sequelize = new Sequelize(database, user, password, {
      host,
      port,
      dialect, //表示何种数据库
      define: { timestamps: false, freezeTableName: true },
      pool: {
        max: 5, //最大连接数
        min: 1, //最小连接数
        acquire: 1000, //sql 查询等待时间
        idle: 10000 //空闲连接等待时间
      }
    })
    // this.addModel()
  }

  addModel() {
    const modelPath = path.join(process.cwd(), 'src/modules/decormodel')
    this.sequelize.addModels([modelPath])
  }
}

const baseDao = BaseDao.baseDao
baseDao.addModel()
// export default BaseDaoOrm.baseDaoOrm
export const { sequelize } = baseDao
